﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using NAutowired.Core.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace IH.Apis.Filters
{
    public class CusExceptionAttribute : ExceptionFilterAttribute
    {
        private readonly ILogger<CusExceptionAttribute> _logger;

        public CusExceptionAttribute(ILogger<CusExceptionAttribute> logger)
        {
            _logger = logger;
        }

        public override void OnException(ExceptionContext context)
        {
            var ex = context.Exception;
            //日志记录
            _logger.LogError(ex, $"{context.ActionDescriptor.DisplayName} 发生异常");
            context.Result = new ContentResult()
            {
                StatusCode = 500,
                Content = ex.Message
            };
        }
    }
}
